//
//  PostClass.m
//  iRMAP
//
//  Created by Ryan Enguero on 4/8/14.
//  Copyright (c) 2014 engueroryan. All rights reserved.
//

#import "PostClass.h"

#import "APIs.h"

#import "Json.h"

static PostClass *sharedItem = nil;

@implementation PostClass

#pragma mark - Login Function

- (NSDictionary*)getUsername:(NSString*)usernameTxt andGetPassword:(NSString*)passwordTxt{

    NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",usernameTxt,passwordTxt];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:logIn];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    return returnData;

}



- (NSDictionary *)getCompanies:(NSString *)sessionId{

     @autoreleasepool {
    NSString *post =[[NSString alloc] initWithFormat:@"sessionid=%@",sessionId];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:listCompanies];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    return returnData;
     }
}

- (NSDictionary*)getDevicesSessionID:(NSString*)sessionID andDomainID:(NSString*)domainID{

     @autoreleasepool {
    NSString *post =[[NSString alloc] initWithFormat:@"sessionid=%@&domainid=%@",sessionID,domainID];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:listDevices];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    return returnData;
     }
}

- (NSDictionary*)getDevicesSessionID:(NSString*)sessionID andDomainID:(NSString*)domainID andDeviceID:(NSString*)deviceID{
    
     @autoreleasepool {
    NSString *post =[[NSString alloc] initWithFormat:@"sessionid=%@&domainid=%@",sessionID,domainID];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:viewLogs];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    return returnData;
     }
}

- (NSDictionary*)getEventLog:(NSString*)sessionID andDomainID:(NSString*)domainID andDeviceID:(NSString*)deviceID{
     @autoreleasepool {
    NSString *post =[[NSString alloc] initWithFormat:@"sessionid=%@&domainid=%@&deviceid=%@",sessionID,domainID,deviceID];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:eventLog];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    return returnData;
     }
    
}


- (NSDictionary*)getAlarmLog:(NSString*)sessionID andDomainID:(NSString*)domainID andDeviceID:(NSString*)deviceID{
    
     @autoreleasepool {
    NSString *post =[[NSString alloc] initWithFormat:@"sessionid=%@&domainid=%@&deviceid=%@",sessionID,domainID,deviceID];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:alarmLog];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    return returnData;
    
     }
}

- (NSDictionary*)getSensorLog:(NSString*)sessionID andDomainID:(NSString*)domainID andDeviceID:(NSString*)deviceID{
    
     @autoreleasepool {
    NSString *post =[[NSString alloc] initWithFormat:@"sessionid=%@&domainid=%@&deviceid=%@",sessionID,domainID,deviceID];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:sensorLog];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    return returnData;
    
     }
}

- (NSDictionary*)logOutWithSessionID:(NSString*)sessionID{

     @autoreleasepool {
    NSString *post =[[NSString alloc] initWithFormat:@"sessionid=%@",sessionID];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:logOut];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    NSLog(@"Log out = %@", returnData);
    
    return returnData;
         
     }

}

- (NSDictionary*)statusAcknowledgeWithSessionID:(NSString*)sessionID andDomainID:(NSString*)domainID andDeviceID:(NSString*)deviceID{

     @autoreleasepool {
    NSString *post =[[NSString alloc] initWithFormat:@"sessionid=%@&domainid=%@&deviceid=%@",sessionID, domainID, deviceID];
    NSLog(@"PostData: %@",post);
    
    NSURL *url=[NSURL URLWithString:statusAcknowledgement];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *returnData = [Json jsonParser:urlData];
    
    NSLog(@"Status Acknowledge = %@", returnData);
    
    return returnData;
         
     }
    
}


#pragma mark - SingleTon

+ (PostClass *)sharedItems
{
    if (sharedItem == nil) {
        sharedItem = [[super allocWithZone:NULL]init];
    }
    return  sharedItem;
}
- (id)init{
    if ((self = [super init])) {
        
    }
    return self;
}
+ (id)allocWithZone:(NSZone *)zone{
    return  [self sharedItems];
}
- (id)copyWithZone:(NSZone *)zone{
    return self;
}


@end
